<?php 

Class goodsModel extends appModel
{
	/**
	 * 获得品牌详细信息
	 * @param $id
	 * @return 
	 */
	public function get_brand($id)
	{
		$sql = 'SELECT * FROM '.$this->table('goods_brand')." WHERE id='".$id."'";
		$res = $this->getRow($sql);
		$arr = hy_dig_filter($res['brand_name'], 3);
		$res['en_name'] = $arr[1];
		$res['brand_name'] = $arr[0];
		return $res;
	}
	public function get_all_brand()
	{
		$sql='SELECT brand_name,id,first_letter FROM '.$this->table('goods_brand')." ORDER BY first_letter";
		$res=$this->getAll($sql);
	       foreach($res as  $k=>$v)
			{
				$res[$k]['brand_name']=hy_dig_filter($v['brand_name'],1);
			}
		return $res;
	}
	
	/**
	 * 获取品牌列表
	 * @param $sort
	 * @param $page
	 * @param $limit
	 * @return
	 */
	public function get_brand_list($sort=array(), $page, $limit = 10)
	{
		$listPage = array('data' => '', 'pageLink' => '');
		$where = ' 1=1';
	
		if($sort['keyword'] != '')
		{
			$where .= " AND brand_name like '%".$sort['keyword']."%'";
		}
		$sql = "SELECT id,brand_name,brand_logo,brand_desc,site_url,ordering,is_show,first_letter FROM ".$this->table('goods_brand')."  WHERE ".$where."  ORDER BY id DESC";
		$result = $this->show_page($sql, $page, $limit);
		if($result['result'])
		{
			foreach($result['result'] as $k => $v)
			{
				$result['result'][$k]['brand_name'] = hy_dig_filter($v['brand_name'], 1);
			}
		}
		return $result;
	}
	
	/**
	 * 获取商品属性详情
	 * @param $id
	 * @return 
	 */
	public function get_attr($id)
	{
		$sql = 'SELECT name,value,thumb,color FROM '.$this->table('goods_attr')." WHERE id='".$id."'";
		$res = $this->getRow($sql);
		$arr = hy_dig_filter($res['name'], 3);
		$res['en_name'] = $arr[1];
		$res['name'] = $arr[0];
		$arr = hy_dig_filter($res['value'], 3);
		$res['en_value'] = explode(',', $arr[1]);
		$res['value'] = explode(',', $arr[0]);
		$res['thumb'] = explode(',', $res['thumb']);
		$res['color'] = explode(',', $res['color']);
		return $res;
	}
	
	/**
	 * 获取商品属性列表信息
	 * @param $page
	 * @return 
	 */
	public function get_attr_list($page, $limit = 10)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where=' 1=1';
	
		$sql="SELECT id,value,name FROM (".$this->table('goods_attr')." AS gt)   WHERE ".$where."  ORDER BY id DESC";
		$result=$this->show_page($sql, $page, $limit);
		if($result['result'])
		{
			foreach($result['result'] as  $k=>$v)
			{
				$result['result'][$k]['name']=hy_dig_filter($v['name'],1);
				$result['result'][$k]['value']=hy_dig_filter($v['value'],1);	
			}
		}
		return $result;
	}
	
	/**
	 * 删除商品属性
	 */
	public function del_attr($id)
	{
		$sql = "delete from ".$this->table('goods_attr')." where id=".$id;
		return $this->query($sql);
	}
	
	/**
	 * 获取商品类型
	 * @param $page
	 * @param $sort
	 * @return 
	 */
	public function get_spec_list($page, $limit = 10)
	{
		$listPage = array('data' => '', 'pageLink' => '');
		$where = ' 1=1';
		
		$sql = "SELECT gt.id,gt.type_name,COUNT(ga.id) as num FROM (".$this->table('goods_type')." AS gt) LEFT JOIN  ".$this->table('goods_spec')." AS ga  ON gt.id=ga.type_id   WHERE ".$where." GROUP BY gt.id ORDER BY gt.id DESC";
		$result = $this->show_page($sql, $page, $limit);
		if($result['result'])
		{
			foreach($result['result'] as  $k=>$v)
			{
				switch($v['type'])
				{
					case 0:$result['result'][$k]['type']='单行文本';break;
					case 1:$result['result'][$k]['type']='多行文本';break;
					case 2:$result['result'][$k]['type']='单选按钮';break;
					case 3:$result['result'][$k]['type']='多选按钮';break;
					case 4:$result['result'][$k]['type']='上传';break;
				}
				$result['result'][$k]['name']=hy_dig_filter($v['name'],1);	
			}
		}
		return $result;
	}
	public function get_all_spec()
	{
		$sql="SELECT id,type_name FROM ".$this->table('goods_type');
		$result=$this->getAll($sql);
		return $result;
	}
	
	/**
	 * 更新类型
	 * @param $tags
	 * @return 
	 */
	public function save_tags($tags)
	{
		$sql = 'SELECT 1 FROM '.$this->table('goods_type')." WHERE type_name='".$tags."'";
		if($this->getOne($sql))
		{
			return false;
		}
		else 
		{
			$sql = 'INSERT  INTO '.$this->table('goods_type')." (type_name) VALUES ('".$tags."')";
			if($this->query($sql))
			{
				return $this->insert_id();
			}
			else 
			{
				return false;
			}
		}
	}
	public function get_spec($id)
	{
		$data=array();
		if($id)
		{
			///$sql="SELECT a.*  FROM ".$this->table('goods_spec')." as a WHERE a.id = '".$id."'";
			//$result=$this->getOne($sql);
			$sql="SELECT a.type_name,a.id,ga.*  FROM ".$this->table('goods_type')." as a LEFT JOIN ".$this->table('goods_spec')." as ga ON ga.type_id=a.id   WHERE a.id = '".$id."'";
			$result=$this->getAll($sql);
			
			$ch_list=array();
			$en_list=array();
			if($result)
			{
				foreach($result as $k=>$v)
				{
					$arr=hy_dig_filter($v['name'],3);
					$arr2=hy_dig_filter($v['field'],3);
					$data['en_list'][$k]=$data['ch_list'][$k]=$v;
					$data['en_list'][$k]['en_field']=$arr2[1];
					$data['ch_list'][$k]['field']=$arr2[0];
					$data['en_list'][$k]['en_name']=$arr[1];
					$data['ch_list'][$k]['name']=$arr[0];
				}
				
				$data['type_name']=$v['type_name'];
				$data['type_id']=$v['type_id'];
				return $data;
			}
			else
			{
				return $data;
			}
		}
	}
	/*
	 * 商品详情
	* */
	public function get_goods_list($page=1,$sort=array())
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where=' 1=1';
		
		if($sort['keyword']!='')
		{
			$where.=" AND goods_name like '%".$sort['keyword']."%'";
		}
		if($sort['cat_id']!='')
		{
			$where.=" AND cat_id = '".$sort['cat_id']."'";
		}
		if($sort['brand_id']!='')
		{
			$where.=" AND brand_id = '".$sort['brand_id']."'";
		}
		$sql="SELECT id,goods_name,ordering,shop_price,goods_num,createtime FROM (".$this->table('goods')." AS gt)   WHERE ".$where."  ORDER BY id DESC";
		if($page<=0)
		{
			$result['result']=$this->getAll($sql);
		}
		else {
			$result=$this->show_page($sql,$page);
		}
		
		if($result['result'])
		{
			foreach($result['result'] as  $k=>$v)
			{
				$result['result'][$k]['goods_name']=hy_dig_filter($v['goods_name'],1);
			}
		}
		return $result;
	}
	public function update_links($id,$links)
	{
		if(count($links)>1)
		{
			$where="AND link_goods not in (".implode(',',$links).')';
		}
		elseif(count($links)==1)
		{
			$where="AND link_goods  <> '".current($links)."'";
		}
		else 
		{
			$where="";
			return true;
		}
	  $sql='DELETE FROM '.$this->table('link_goods')." WHERE goods_id = '".$id."' ".$where;
	  $res=$this->query($sql);
	  
	  return $res;
	}
	public function get_goods($id)
	{
		$sql='SELECT * FROM '.$this->table('goods')." WHERE id='".$id."'";
		$res=$this->getRow($sql);
		$arr=hy_dig_filter($res['goods_name'],3);
		$res['en_name']=$arr[1];
		$res['goods_name']=$arr[0];
		$arr=hy_dig_filter($res['goods_desc'],3);
		$res['en_desc']=$arr[1];
		$res['goods_desc']=$arr[0];
		$arr=hy_dig_filter($res['goods_brief'],3);
		$res['en_brief']=$arr[1];
		$res['goods_brief']=$arr[0];
		//关联商品
	$sql='SELECT lg.id,g.shop_price,g.goods_num,g.goods_name,g.id FROM '.$this->table('link_goods').' as lg,'.$this->table('goods')." as g WHERE lg.link_goods=g.id AND lg.goods_id='".$id."'";
	    $result= $this->getAll($sql);
	    foreach($result as $k=>$v)
	    {
	    	$result[$k]['goods_name']=hy_dig_filter($v['goods_name'],1);
	    	
	    }  
		$res['link_goods']=$result;
		//获取相册集
		$sql="SELECT p.* FROM ".$this->table('goods_gallery')." as p WHERE p.goods_id = '".$id."'";
		
		$photo=$this->getAll($sql);
		$res['photo']=$photo;
		//获取商品属性
		$sql='SELECT ge.id,ge.attr_value,ge.goods_img,ga.name FROM '.$this->table('goods_attr_extend').' as ge,'.$this->table('goods_attr')." as ga WHERE ga.id=ge.attr_id AND ge.goods_id='".$id."'";
		$attr= $this->getAll($sql);
		foreach($attr as $k=>$v)
		{
			$attr[$k]['name']=hy_dig_filter($v['name'],1);;
			$temp=(array)json_decode($v['attr_value']);
			$attr[$k]['attr_name']=hy_dig_filter(urldecode($temp['name']),1);;
		}
			
		$res['attr_list']=$attr;
		//获取规格-start
		$spec_arr=$this->get_goods_spec($res['goods_type']);	
		$sql='SELECT id,spec_id,spec_value FROM '.$this->table('goods_spec_extend')." WHERE goods_id='".$id."'";
		$temp=$this->getAll($sql);
		foreach($temp as $k=>$v)
		{
			$goods_spec[$v['spec_id']]['value']=$v['spec_value'];
			$goods_spec[$v['spec_id']]['id']=$v['id'];
		}
		
		foreach($spec_arr as $k=>$v)
		{
			if($goods_spec[$v['id']])
			{
				if($spec_arr[$k]['field_list'])
				{//可选择类型
				  foreach($spec_arr[$k]['field_list'] as $kk=>$vv)
				  {
					if($vv['val']==$goods_spec[$v['id']]['value'])
					{
						$spec_arr[$k]['field_list'][$kk]['checked']=1;
					}
				  }
				}
				else 
				{//输入框类型
						$spec_arr[$k]['field']=$goods_spec[$v['id']]['value'];
				}
			}
		}
		if(!empty($spec_arr))
		{
			$html='';
			$tpl='<div class="am-g am-margin-top"><div class="am-u-sm-2 am-text-right">%s</div><div class="am-u-sm-4 am-u-end">%s</div></div>';
			foreach($spec_arr as $k=>$v)
			{
				if($v['type']==1)
				{
					$input='<div class="am-btn-group" data-am-button>';
					foreach($v['field_list'] as $kk=>$vv)
					{
						$checked='';
						$class='';
						if($vv['checked'])
						{
							$class='am-active';
							$checked='checked';
						}
						$input.='<label class="am-btn am-btn-default am-btn-xs '.$class.'"><input '.$checked.' type="radio" name="val_spec_'.$v['id'].'" value="'.$vv['val'].'"  >'.$vv['name'].'</label>';
					}
					$input.='</div>';
				}
				elseif($v['type']==2)
				{
					$ch_txt=hy_dig_filter($v['field'],1);
					$en_txt=hy_dig_filter($v['field'],2);
					$input='<input type="text" value="'.$ch_txt.'" name="val_spec_'.$v['id'].'" />';
					if(DIGLOSSIA)
					{
						$input.='<br>';
						$input.='<input type="text" value="'.$en_txt.'" name="val_spec_'.$v['id'].'" />';
					}
				}
				elseif($v['type']==3)
				{
					$input='<div class="am-btn-group" data-am-button>';
					foreach($v['field_list'] as $kk=>$vv)
					{
						$input.='<label class="am-btn am-btn-default am-btn-xs '.$class.'"><input '.$checked.' type="checkbox" name="val_spec_'.$v['id'].'" value="'.$vv['val'].'"  >'.$vv['name'].'</label>';
					}
					$input.='</div>';
				}
				$html.=sprintf($tpl,'<input type="hidden" value="'.$v['name'].'" name="spec_'.$v['id'].'" /><input type="hidden" value="'.$goods_spec[$v['id']]['id'].'" name="goods_spec_id" />'.$v['name'],$input);
			}
			$res['spec']=$html;
		}
		//获取规格-end
		return $res;
	
	}
	public function get_all_attr()
	{
		$sql="SELECT id,name,value,thumb,color FROM ".$this->table('goods_attr');
		$result=$this->getAll($sql);
		foreach($result as $k=>$v)
		{
			$result[$k]['name']=hy_dig_filter($v['name'],1);
			$temp=hy_dig_filter($v['value'],1);
			$en_name=hy_dig_filter($v['value'],2);
			$en_name=explode(',',$en_name);
			$thumb=explode(',',$v['thumb']);
			$color=explode(',',$v['color']);
			$temp=explode(',',$temp);
			$temp_value=array();
			foreach($temp as $kk=>$vv)
			{
				
				$temp_value[$kk]['name']=$vv;
				$temp2['name']=urlencode(CH_SC.$vv.DIG_EC.EN_SC.$en_name[$kk].DIG_EC);
				$temp2['thumb']=$thumb[$kk];
				$temp2['color']=$color[$kk];
				$temp_value[$kk]['value']=json_encode($temp2);
			}
			$result[$k]['value']=$temp_value;
		}
		return $result;
	}
	public function get_goods_attr($id)
	{
		$sql='SELECT name,value,thumb,color FROM '.$this->table('goods_attr')." WHERE id='".$id."'";
		$res=$this->getAll($sql);
		foreach($res as $k=>$v)
		{
			$res[$k]['name']=hy_dig_filter($v['name'],1);
			$ch_field=hy_dig_filter($v['field'],1);
			$en_field=hy_dig_filter($v['field'],2);
				
			if($v['type']==1||$v['type']==3)
			{
				$temp=explode(',',$ch_field);
				$temp2=explode(',',$en_field);
	
				foreach($temp as $kk=>$vv)
				{
					$res[$k]['field_list'][$kk]['id']=$v['id'];
					$res[$k]['field_list'][$kk]['val']=CH_SC.$vv.DIG_EC.EN_SC.$temp2[$kk].DIG_EC;
					$res[$k]['field_list'][$kk]['name']=$vv;
				}
			}
				
		}
		return $res;
	}
	public function get_goods_spec($id)
	{
		$sql='SELECT id,name,type,field  FROM '.$this->table('goods_spec')." WHERE type_id='".$id."'";
		$res=$this->getAll($sql);
		foreach($res as $k=>$v)
		{
			$res[$k]['name']=hy_dig_filter($v['name'],1);
			$ch_field=hy_dig_filter($v['field'],1);
			$en_field=hy_dig_filter($v['field'],2);
			
			if($v['type']==1||$v['type']==3)
			{
				$temp=explode(',',$ch_field);
				$temp2=explode(',',$en_field);
				
				foreach($temp as $kk=>$vv)
				{
					$res[$k]['field_list'][$kk]['id']=$v['id'];
					$res[$k]['field_list'][$kk]['val']=CH_SC.$vv.DIG_EC.EN_SC.$temp2[$kk].DIG_EC;
					$res[$k]['field_list'][$kk]['name']=$vv;
				}
			}
			
		}
		return $res;
	}
	public function ordering($val,$id)
	{
		$result=array('error'=>0,'content'=>'');
		$sql="UPDATE ".$this->table('product')." SET ordering='$val' WHERE id='".$id."'";
		$res=$this->query($sql);
		if(!res)
		{
			$result['error']=1;
			$result['content']='';
		}
		return $result;
	}
	public function del_pro($id)
	{
		$result=array('error'=>0,'content'=>'');
		if($id)
		{
			
				$sql="DELETE FROM ".$this->table('product')." WHERE  id='".$id."'";
	
				$res=$this->query($sql);
				 
				$sql="DELETE FROM ".$this->table('product_img')." WHERE  product_id='".$id."'";
				
				$res2=$this->query($sql);
				if(!$res||!$res2)
				{
					$result['error']=1;
					$result['content']='删除失败';
				}
	
	
			
			 
	
	
		}
		return $result;
	}
	public function get_pro_img($id)
	{
		$sql="SELECT original_pic,id FROM ".$this->table('product_img')." WHERE product_id = '".$id."'";
			
		$result=$this->getAll($sql);
		if($result)
		{
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	public function get_pro($id)
	{
		$sql="SELECT * FROM ".$this->table('product')." WHERE id = '".$id."'";
			
		$result=$this->getRow($sql);
		if($result)
		{
			return $result;
		}
		else
		{
			return $result=array();
		}
	}
	public function update_pro($info,$img_array,$id)
	{
		$result=array('error'=>0,'content'=>'');
		if($id)
		{
		
			foreach($info as $k=>$v)
			{
				
					$where.=" $k='$v',";
						
				
			}
			
			$where.="modified='".$_SERVER['REQUEST_TIME']."'";
		
			$sql="UPDATE ".$this->table('product')." SET ".$where." WHERE id='".$id."'";
			
		    $res=$this->query($sql);
		    if(!$res)
		    {
		    	$result['error']=1;
		    	$result['content']=Lang('SQL_ERROR');
		    }
		    
		    
		    $sql="SELECT id FROM ".$this->table('product_img')." WHERE product_id='".$id."'";
		    $old_pic=$this->getCol($sql);
		  
		   
		   
		    //删除图片;
		    foreach($old_pic as $k1=>$v1)
		    {
		    	if(!in_array($v1,array_flip($img_array)))
		    	{
		    		
		    		$sql="DELETE FROM ".$this->table('product_img')." WHERE  id='".$v1."'";
		    		$res=$this->query($sql);
		    		
		    		if(!$res)
		    		{
		    			$result['error']=2;
		    			$result['content']=Lang('SQL_ERROR');
		    		}
		    		
		    	}
		    }
		    
		    foreach($img_array as $kk=>$vv)
		    {
		    		if($kk>0)//更新
		    		{
		    			
		    			if(in_array($kk,$old_pic))

		    				$sql="SELECT original_pic FROM ".$this->table('product_img')." WHERE id='".$kk."'";
		    				$old_pic_url=$this->getOne($sql);
		    				if($vv!=$old_pic_url)
		    				{
		    					if(is_file(ROOT_PATH."/".$vv))
		    					{
		    						$middle_pic=$this->make_thumb('/'.$vv,'200','200');
		    						$small_pic=$this->make_thumb('/'.$vv,'80','80');
		    					}
		    			        $sql="UPDATE ".$this->table('product_img')." SET original_pic='".$vv."',middle_pic='".$middle_pic."',small_pic='".$small_pic."'  WHERE id='".$kk."'";
		    			
		    			       $res=$this->query($sql);
		    				}
		    		  
		    		}
		    		else //新增
		    		{
		    			if(is_file(ROOT_PATH."/".$vv))
		    			{
		    				$middle_pic=$this->make_thumb('/'.$vv,'200','200');
		    				$small_pic=$this->make_thumb('/'.$vv,'80','80');
		    			}
		    			$sql="INSERT INTO ".$this->table('product_img')." (product_id,original_pic,middle_pic,small_pic) VALUES('$id','$vv','$middle_pic','$small_pic')";
		    			$res=$this->query($sql);
		    			
		    			if(!$res)
		    			{
		    				$result['error']=3;
		    				$result['content']=Lang('SQL_ERROR');
		    			}
		    		}
				
		    }
		    
		    
		}
		return $result;
	}
	public function create_pro($info,$img_array)
	{
		$field='';
		$value='';
		$result=array('error'=>0,'content'=>'');
		
		
		
			
			foreach($info as $k=>$v)
			{
				if($v!='')
				{
					$field.="$k,";
						
						
					$value.="'$v',";
			
				}
			}
			
			
			$field=substr($field,0,strlen($field)-1);
			$value=substr($value,0,strlen($value)-1);
			
			$sql="INSERT INTO ".$this->table('product')." (".$field.") VALUES(".$value.")";
			if(!$res=$this->query($sql))
			{
				$result['error']=1;
				$result['content']=Lang('SQL_ERROR');
			}
			else 
			{//添加产品图片
				$product_id=$this->insert_id();
				if(count($img_array)>0)
				{
					foreach($img_array as $key=>$val)
					{
						
						
						if(is_file(ROOT_PATH."/".$val))
						{
							$middle_pic=$this->make_thumb('/'.$val,'200','200');
							$small_pic=$this->make_thumb('/'.$val,'80','80');
						}
						$sql="INSERT INTO ".$this->table('product_img')." (product_id,original_pic,middle_pic,small_pic) VALUES('$product_id','$val','$middle_pic','$small_pic')";
						if(!$res=$this->query($sql))
						{
							$result['error']=1;
							$result['content']=Lang('SQL_ERROR');
						}
					}
						
				}
			}
		
	
		return $result;
		//$this->make_thumb('/Uploads/ad/1398673595657549402.jpg','200','200');
	}
	public function pro_list($page=1,$cat_id)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		
		$result=$this->show_page("SELECT id,title,ordering FROM ".$this->table('product').' WHERE cat_id='.$cat_id,$page);
		$listPage['pagelink']=$result['page'];
		$listPage['data']=$result['result'];
		
		
		return $listPage;
	}

	
	
	
}